<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of m_diemrenluyen
 *
 * @author trung_000
 */
class M_diemrenluyen extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    public function them($Ma_SV, $data) {
        date_default_timezone_set("Asia/Ho_Chi_Minh");
        $this->db->where('HocKy', $data['HocKy']);
        $this->db->where('FK_Ma_SV', $Ma_SV);

        $q = $this->db->get('tbl_diemrenluyen');
        if ($q->num_rows() > 0) {
            return FALSE;
        }
        $this->db->set('FK_Ma_SV', $Ma_SV);
        $this->db->set('HocKy', $data['HocKy']);
        $this->db->set('KetQuaHocTap', $data['KetQuaHocTap']);
        $this->db->set('ThamGiaHocTap', $data['ThamGiaHocTap']);
        $this->db->set('ChapHanhQuyChe', $data['ChapHanhQuyChe']);

        $this->db->set('ThamGiaNghienCuKhaoHoc', $data['ThamGiaNghienCuKhaoHoc']);
        $this->db->set('ChapHanhNoiQuy', $data['ChapHanhNoiQuy']);
        $this->db->set('HoatDongCTXHVH', $data['HoatDongCTXHVH']);
        $this->db->set('PhamChatCongDan', $data['PhamChatCongDan']);
        $this->db->set('QuanLyLop', $data['QuanLyLop']);
        $this->db->set('TrangThai', 0);
        return $this->db->insert('tbl_diemrenluyen');
    }

    public function capnhat($data) {
        $this->db->where('id', $data['id']);
        $this->db->set('KetQuaHocTap', $data['KetQuaHocTap']);
        $this->db->set('ThamGiaHocTap', $data['ThamGiaHocTap']);
        $this->db->set('ChapHanhQuyChe', $data['ChapHanhQuyChe']);
        $this->db->set('ThamGiaNghienCuKhaoHoc', $data['ThamGiaNghienCuKhaoHoc']);
        $this->db->set('ChapHanhNoiQuy', $data['ChapHanhNoiQuy']);
        $this->db->set('HoatDongCTXHVH', $data['HoatDongCTXHVH']);
        $this->db->set('PhamChatCongDan', $data['PhamChatCongDan']);
        $this->db->set('QuanLyLop', $data['QuanLyLop']);
        $this->db->set('TrangThai', 1);
        return $this->db->update('tbl_diemrenluyen');
    }
    
    public function duyet($id,$TrangThai) {
        $this->db->where('id', $id);
        $this->db->set('TrangThai', $TrangThai);
        return $this->db->update('tbl_diemrenluyen');
    }

    function tinhdiem($row) {
        return $row['KetQuaHocTap'] + $row['ThamGiaHocTap'] + $row['ChapHanhQuyChe'] + $row['ThamGiaNghienCuKhaoHoc'] + $row['ChapHanhNoiQuy'] + $row['HoatDongCTXHVH'] + $row['PhamChatCongDan'] + $row['QuanLyLop'];
    }

    function tinh($diem) {
        if ($diem > 90) {
            return 'Xuất sắc';
        } elseif ($diem > 80) {
            return 'Tốt';
        } elseif ($diem > 70) {
            return 'Khá';
        } elseif ($diem > 60) {
            return 'Trung bình khá';
        } elseif ($diem > 50) {
            return 'Trung binh';
        } elseif ($diem > 30) {
            return ' Yếu';
        } else {
            return ' Kém';
        }
    }

    public function layketqua($Ma_SV) {
        $this->db->where("FK_Ma_SV", $Ma_SV);
        $this->db->order_by('HocKy', "desc");

        $query = $this->db->get('tbl_diemrenluyen');
        $dsxeploai = FALSE;
        foreach ($query->result_array() as $row) {
            $tamp['HocKy'] = $row['HocKy'];
            $tamp['Diem'] = $this->tinhdiem($row);
            $tamp['XepLoai'] = $this->tinh($tamp['Diem']);
            $dsxeploai[] = $tamp;
        }
        return $dsxeploai;
    }

    function getOneId($id) {
        $this->db->where('id', $id);
        $this->db->join('tbl_sinh_vien', 'Ma_SV=FK_Ma_SV');
        return $this->db->get('tbl_diemrenluyen')->row_array();
    }

    public function laylistketqua($Lop, $ky) {
        
    }

    function getAll() {

        $this->db->order_by('HocKy', "desc");
        $this->db->order_by('TrangThai', "desc");
        $this->db->order_by('Lop_SV', "desc");
       
        $this->db->join('tbl_sinh_vien', 'Ma_SV=FK_Ma_SV');

        $query = $this->db->get('tbl_diemrenluyen');
        $data = FALSE;
        foreach ($query->result_array() as $key => $row) {
            $arr = $row;

            $arr['Diem'] = $this->tinhdiem($row);
            $arr['XepLoai'] = $this->tinh($arr['Diem']);
            $data[] = $arr;
        }
        return $data;
    }

    function thoidiemdanhgia() {
        $this->db->select_max('ThoiGian');
        $query = $this->db->get('tbl_kyrenluyen');
        $data = $query->row_array();
        if ($data['ThoiGian'])
            return $data;
    }

}
